﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLTrinhDo
    {
        List<TrinhDo> list;
        QuanLySVDataContext db;


        // constructor
        public BLLTrinhDo()
        {
            this.list = new QuanLySVDataContext().TrinhDos.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 trinh do co ton tai hay khong
        public bool checkExist(TrinhDo temp)
        {
            foreach (TrinhDo dt in list)
            {
                if (dt.TrinhDoID.Equals(temp.TrinhDoID))
                    return true;
            }
            return false;
        }

        // update thong tin trinh do
        public bool updateTrinhDo(TrinhDo update)
        {
            try
            {

                var temp = (from c in db.TrinhDos
                            where c.TrinhDoID == update.TrinhDoID
                            select c).First();
                TrinhDo found = (TrinhDo)temp;
                found.TrinhDoID = update.TrinhDoID;
                found.TenTrinhDo = update.TenTrinhDo;
                found.GhiChu = update.GhiChu;

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them trinh do moi vao database
        public bool insertTrinhDo(TrinhDo temp)
        {
            try
            {
                db.TrinhDos.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa trinh do
        public bool deleteTrinhDo(TrinhDo temp)
        {
            try
            {
                var dt = (from c in db.TrinhDos
                          where c.TrinhDoID == temp.TrinhDoID
                          select c).First();
                TrinhDo found = (TrinhDo)dt;

                db.TrinhDos.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac trinh do
        public List<TrinhDo> returnListTrinhDo()
        {
            this.list = new QuanLySVDataContext().TrinhDos.ToList();
            return list;
        }

        // tra ve 1 dan toc theo so thu tu trong list
        public TrinhDo retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().TrinhDos.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else return null;
        }

        // tra ve 1 dan toc theo ma dan toc
        public TrinhDo retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().TrinhDos.ToList();
            foreach (TrinhDo temp in list)
                if (temp.TrinhDoID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 dan toc co hop le hay khong
        public bool kiemTra(TrinhDo temp)
        {
            if (temp.TrinhDoID == "") return false;
            return true;
        }
    }
}
